package com.acme.bankapp.service;
import com.acme.bankapp.domain.*;

import java.io.*;
import java.util.StringTokenizer;


public class BankDataLoaderService {


	/**
	 * Loads a file which contains data feed in the following format: <br>
	 * accounttype=c|s;balance=100;overdraft=50;name=John;gender=m|f;
	 * 
	 * @param bank
	 * @param path
	 * @throws IOException 
	 */
	public static void load(final Bank bank, final String path)
			throws IOException {
		BufferedReader bis = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
		String line;
		while ((line = bis.readLine()) != null) {
			StringTokenizer strToken = new StringTokenizer(line);
			Account account;
			boolean accTypeIsChecked = strToken.nextToken(";").endsWith("c");
			strToken.nextToken("=");
			String balance = strToken.nextToken(";").substring(1);
			if (accTypeIsChecked) {
				strToken.nextToken("=");
				account = new CheckingAccount(Double.parseDouble(balance),Double.parseDouble(strToken.nextToken(";").substring(1)));
			} else {
				account = new SavingAccount(Double.parseDouble(balance));
			}
			strToken.nextToken("=");
			String name = strToken.nextToken(";").substring(1);
			strToken.nextToken("=");
			String gender = strToken.nextToken(";").substring(1);
			Client client = new Client();
			if ("m".equals(gender)) {
				client = new Client(name, Gender.MALE);
			} else
			if ("f".equals(gender)) {
				client = new Client(name, Gender.FEMALE);
			}
			client.addAccount(account);
			System.out.println("Client Added: "+client);
			
			
			
		}
		bis.close();
	}


}
